/*

July 2012
Meredith Fowlie
fowlie@berkeley.edu

Objective : Generate estimation results reported in Table 2. 

*** Input files required to run this program

	"biannual_data.dta"
	
*/

clear matrix
clear
set more off
set mem 1000m

*cd T:\RECLAIM_FINAL\DATA_APPENDIX\DATA_APPENDIX_RESTAT

*CHANGE TO LOCAL DIRECTORY HERE

use "biannual_data.dta"

* drop EGUs that were excluded from the program post 2000 *

drop if sic_final==4911 & year>2000

duplicates drop

drop if year>2004
gen c=1
egen sum=sum(c), by (facid)
gen BAL=0
replace BAL=1 if sum>20

gen IV=0
replace IV=1 if cycle==2 & half==1

gen IVcheck=0
replace IVcheck=1 if cycle==2 & half==2
replace IVcheck=0 if year>2004

drop if year>2004

drop if DIFF1==.
drop if DIFF1==0

gen lnemi=ln(sumemi)

drop if lnemi==0

gen lna=ln(suma)

gen lnwage=ln(mean_wage)

gen lnppi=ln(mean_ppi)

xi i.period

gen ln_a_hat=log(a_hat)

capture drop c sum
gen c=1
replace c=0 if lnemi==.
replace c=0 if lna==.

egen sum=sum(c), by(facid)
drop if sum<2

sort year
egen group=group(year)
gen t=group-1


/* Table 2 */

reg lnemi lna _Iperiod_*, robust cluster(facid)

reg lnemi lna lnPT1 lnDIFF1 _Iperiod_*, robust cluster(facid)

reg lnemi lna lnwage lnppi lnPT1 lnDIFF1 _Iperiod_*, robust cluster(facid)

* xtreg lnemi lna  _Iperiod_* , fe i(facid) robust cluster(facid)

areg lnemi lna  _Iperiod_* , absorb(facid) robust cluster(facid)


/* Table 3 */

ivreg2 lnemi (lna=IV) _Iperiod_* lnPT1 lnDIFF1, cluster(facid) ffirst

xtivreg2 lnemi (lna=IV) _Iperiod_* , fe i(facid) robust cluster(facid)ffirst

/* test for endog */

xtivreg2 lnemi (lna=IV) _Iperiod_*, fe i(facid)  robust cluster(facid) endogtest(lna)

/* First stage */

reg lna IV  lnPT1 lnDIFF1 _Iperiod_* , robust cluster(facid)
test IV

areg lna IV _Iperiod_* , absorb(facid) robust cluster(facid)
test IV

save temp, replace

drop if lnemi==.
drop if lna==.
keep facid fac_id
duplicates drop
save "T:\RECLAIM_PERLOFF_FINAL\DATA\biannual_census.dta", replace

use temp

/* balanced panel - robustness section*/

keep if BAL==1

xtreg lnemi lna  _Iperiod_* , fe i(facid) robust cluster(facid)

xtivreg2 lnemi (lna=IV) _Iperiod_* , fe i(facid) robust cluster(facid) 



